<!-- Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>VSAQ - All JsUnit Tests</title>
<script src="javascript/closure/base.js"></script>
<script src="deps-runfiles.js"></script>
<script src="build/all_tests.js"></script>
<script>
goog.require('goog.testing.MultiTestRunner');
</script>
<link rel="stylesheet" href="javascript/closure/css/multitestrunner.css" type="text/css">
<style>
h1 {
  font: normal x-large arial, helvetica, sans-serif;
  margin: 0;
}
p, form {
  font: normal small sans-serif;
}
#form {
  margin-top: 8px;
}
a {
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}
.warning {
  font-family: sans-serif;
  font-size: 14px;
  font-weight: bold;
  padding-right: 8em;
  padding-left: 8em;
  padding-top: 1em;
  padding-bottom: 1em;
  background: #eef;
  border: 1px solid #ccc
}
</style>
</head>
<body>
<h1>VSAQ - All JsUnit Tests</h1>
<div id="runner"></div>
<!--  Use a form so browser persists input values -->
<form id="form">
  Settings:<br>
  <input type="checkbox" name="showpasses" id="showpasses">
  <label for="showpasses">Show passes</label><br>
  <label for="filter">Only run tests that match:</label><br>
  <input type="text" name="filter" id="filter" value="">
  <small>(use regexp, e.g. "structs.*")</small>
</form>
<p>Tests:</p>
<pre id="all_tests"></pre>
<script>
var params = {};
var q = location.search.replace(/^\?/, '').split('&').map(function(item) {
    var param = item.split('=').map(decodeURIComponent);
    params[param[0]] = param[1];
});

if (params.showpasses) {
  document.getElementById('showpasses').checked = true;
}

if (params.filter) {
  document.getElementById('filter').value = params.filter;
}

if (typeof _allTests == 'undefined') {
  document.write(
      '<p>No test files detected.  Ensure you have generated the ' +
      'test dependencies</p>');

} else {
  for (var i = 0; i < _allTests.length; i++) {
    document.getElementById('all_tests').innerText += _allTests[i] + '\n';
  }
  var showPassesInput = document.getElementById('showpasses');
  var filterInput = document.getElementById('filter');

  function setFilterFunction() {
    var matchValue = filterInput.value || '.*';
    var re;
    try {
      re = new RegExp(matchValue);
    } catch (e) {
      alert(e.message);
      setTimeout(function() {
        filterInput.select();
      }, 0);
      return;
    }
    testRunner.setFilterFunction(function(testPath) {
      return re.test(testPath);
    });
  }

  goog.testing.MultiTestRunner.prototype.setOnTestLoadCallback = function(fn) {
    this.onTestLoadCallback = fn;
  };

  goog.testing.MultiTestRunner.prototype.getOnTestLoadCallback = function(fn) {
    return this.onTestLoadCallback;
  };

  // setting a hook for polyfill
  goog.testing.MultiTestRunner.TestFrame.prototype.onIframeLoaded_ = function(e) {
    this.iframeLoaded_ = true;
    if (this.getParent().onTestLoadCallback) {
      this.getParent().onTestLoadCallback.apply(e.target);
    }
  };

  // Create a test runner and render it.
  var testRunner = new goog.testing.MultiTestRunner()
      .setName(document.title)
      .setBasePath('')
      .setPoolSize(1)
      .setStatsBucketSizes(5, 500)
      .setHidePasses(!showPassesInput.checked)
      .addTests(_allTests);
  testRunner.render(document.getElementById('runner'));

  goog.events.listen(showPassesInput, 'click', function(e) {
    testRunner.setHidePasses(!e.target.checked);
  });

  goog.events.listen(filterInput, 'blur', setFilterFunction);
  setFilterFunction();


  if (params.autostart) {
    testRunner.start();
  }
}
</script>
</body>
</html>
